﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ZLDC.Topo.Model.Estate;
using ZLDC.Common.Estate;

namespace ZLDC.DAL.Estate
{
                public class AuthorizationDAL
    {
                                                                                                                                                                                                        public static void p_authorization_save(int deptId, string dept, string useage, string areas, string areasTel, string areasFollow, string areasInfo, string estates, string estatesTel, string estatesFollow, string estatesInfo, string cusTel, string cusFollow, string mark1, string mark2, string mark3, string mark4, string mark5, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@deptId", deptId));
            parms.Add(new SqlParameter("@dept", dept));
            parms.Add(new SqlParameter("@useage", useage));
            parms.Add(new SqlParameter("@areas", areas));
            parms.Add(new SqlParameter("@areasTel", areasTel));
            parms.Add(new SqlParameter("@areasFollow", areasFollow));
            parms.Add(new SqlParameter("@areasInfo", areasInfo));
            parms.Add(new SqlParameter("@estates", estates));
            parms.Add(new SqlParameter("@estatesTel", estatesTel));
            parms.Add(new SqlParameter("@estatesFollow", estatesFollow));
            parms.Add(new SqlParameter("@estatesInfo", estatesInfo));
            parms.Add(new SqlParameter("@cusTel", cusTel));
            parms.Add(new SqlParameter("@cusFollow", cusFollow));
            parms.Add(new SqlParameter("@mark1", mark1));
            parms.Add(new SqlParameter("@mark2", mark2));
            parms.Add(new SqlParameter("@mark3", mark3));
            parms.Add(new SqlParameter("@mark4", mark4));
            parms.Add(new SqlParameter("@mark5", mark5));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_authorization_save", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }
        public static void p_authorization_save1(int deptId, string dept, string areas, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@deptId", deptId));
            parms.Add(new SqlParameter("@dept", dept));
            parms.Add(new SqlParameter("@areas", areas));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_authorization_save1", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }


        public static void p_saveusage(int deptId, string dept, string useage,string mark1, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@deptId", deptId));
            parms.Add(new SqlParameter("@dept", dept));
            parms.Add(new SqlParameter("@useage", useage));
            parms.Add(new SqlParameter("@mark1", mark1));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_saveusage", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }



                                                                public static DataTable p_authorization_detail_table(int typeid, int deptId, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@typeid", typeid));
            parms.Add(new SqlParameter("@deptId", deptId));
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_authorization_detail", parms);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

                                                public static AuthorizationInfo p_authorization_detail(int deptId, _Database db)
        {
            AuthorizationInfo c = new AuthorizationInfo();
            DataTable dt = null;
            DataSet ds = null;

            try
            {
                string sql = "select * from t_authorization where deptId = " + deptId;
                ds = db.Query(sql);

                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    
                    c.ID = (dt.Rows[0]["ID"] == System.DBNull.Value) ? 0 : Convert.ToInt32(dt.Rows[0]["ID"].ToString());
                    c.DeptId = (dt.Rows[0]["deptId"] == System.DBNull.Value) ? 0 : Convert.ToInt32(dt.Rows[0]["deptId"].ToString());
                    c.ModifyDate = (dt.Rows[0]["modifyDate"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(dt.Rows[0]["modifyDate"].ToString());
                    c.Dept = (dt.Rows[0]["dept"] == System.DBNull.Value) ? "" : dt.Rows[0]["dept"].ToString();
                    c.Useage = (dt.Rows[0]["useage"] == System.DBNull.Value) ? "" : dt.Rows[0]["useage"].ToString();
                    c.Areas = (dt.Rows[0]["areas"] == System.DBNull.Value) ? "" : dt.Rows[0]["areas"].ToString();
                    c.AreasTel = (dt.Rows[0]["areasTel"] == System.DBNull.Value) ? "" : dt.Rows[0]["areasTel"].ToString();
                    c.AreasFollow = (dt.Rows[0]["areasFollow"] == System.DBNull.Value) ? "" : dt.Rows[0]["areasFollow"].ToString();
                    c.AreasInfo = (dt.Rows[0]["areasInfo"] == System.DBNull.Value) ? "" : dt.Rows[0]["areasInfo"].ToString();
                    c.Mark1 = (dt.Rows[0]["mark1"] == System.DBNull.Value) ? "" : dt.Rows[0]["mark1"].ToString();
                    c.Mark2 = (dt.Rows[0]["mark2"] == System.DBNull.Value) ? "" : dt.Rows[0]["mark2"].ToString();
                    c.Mark3 = (dt.Rows[0]["mark3"] == System.DBNull.Value) ? "" : dt.Rows[0]["mark3"].ToString();
                    c.Mark4 = (dt.Rows[0]["mark4"] == System.DBNull.Value) ? "" : dt.Rows[0]["mark4"].ToString();
                    c.Mark5 = (dt.Rows[0]["mark5"] == System.DBNull.Value) ? "" : dt.Rows[0]["mark5"].ToString();
                    c.Estates = (dt.Rows[0]["estates"] == System.DBNull.Value) ? "" : dt.Rows[0]["estates"].ToString();
                    c.EstatesTel = (dt.Rows[0]["estatesTel"] == System.DBNull.Value) ? "" : dt.Rows[0]["estatesTel"].ToString();
                    c.EstatesFollow = (dt.Rows[0]["estatesFollow"] == System.DBNull.Value) ? "" : dt.Rows[0]["estatesFollow"].ToString();
                    c.EstatesInfo = (dt.Rows[0]["estatesInfo"] == System.DBNull.Value) ? "" : dt.Rows[0]["estatesInfo"].ToString();
                    c.CusTel = (dt.Rows[0]["cusTel"] == System.DBNull.Value) ? "" : dt.Rows[0]["cusTel"].ToString();
                    c.CusFollow = (dt.Rows[0]["cusFollow"] == System.DBNull.Value) ? "" : dt.Rows[0]["cusFollow"].ToString();
                }
                else
                    return null;
            }
            catch { }
            finally { db.close(); }

            return c;
        }

                                                                public static void p_authorization_delete(int typeid, int deptId, string id, out string outmsg, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@typeid", typeid));
            parms.Add(new SqlParameter("@deptId", deptId));
            parms.Add(new SqlParameter("@id", id));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            db.execute_procedure("p_authorization_delete", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);

        }

        public static string GetAreaCust(string deptid, _Database db)
        {
            DataTable dt = null;
            DataSet ds = null;
            string  sAreaDetpId = string.Empty;

            try
            {
                string sql = "declare @parentid int";
                            sql += "    select top 1 @parentid =deptId from t_authorization where deptId in (";
                            sql += "    select deptid from dbo.f_GetAllFatherDeptAll('" + deptid + "')) and cast(mark1 as int)>0 order by mark1 desc";
                            sql += "     select deptid from f_GetAllChildDeptAll(@parentid)";
                ds = db.Query(sql);

                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        sAreaDetpId += dt.Rows[i]["deptId"] + ",";
                    }
                   
                }
               
            }
            catch { }
            finally { db.close(); }

            return sAreaDetpId.TrimEnd(',');

        }


        

    }
}
